using System;
using System.Collections.Generic;
using System.Linq;

namespace JapaneseLearning.Tools
{
    public static class ArrayExtensions
    {
        public static T[] Shuffle<T>(this T[] array, int? seed = null)
        {
            var random = seed.HasValue ? new Random(seed.Value) : new Random(Guid.NewGuid().GetHashCode());
            var result = array.ToArray();

            for (var i = result.Length - 1; i > 0; i--)
            {
                var j = random.Next(0, i + 1);
                (result[j], result[i]) = (result[i], result[j]);
            }

            return result;
        }

        public static void Shuffle<T>(this List<T> array, int? seed = null)
        {
            var random = seed.HasValue ? new Random(seed.Value) : new Random(Guid.NewGuid().GetHashCode());

            for (var i = array.Count - 1; i > 0; i--)
            {
                var j = random.Next(0, i + 1);
                (array[j], array[i]) = (array[i], array[j]);
            }
        }
    }
}